﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using CrystalDecisions.Shared;

namespace InventoryManager
{
    public partial class ShowReportForm : Form
    {
        private const int ORDERFORM_REPORT = 1;
        private const int REQUISITIONFORM_REPORT = 2;
        private const int TAXDELIVERYFORM_REPORT = 3;
        private const int RECEIPTFORM_REPORT = 4;
        //private const int MERCHANDISESUMMARYFORM_REPORT = 5;

        private int _reportType;
        private int _ID;
        private bool _configPrice;
        private bool _configWeight;
        private string _branchName;

        private string _connectionString;
        private MySqlConnection _connection;
        private MySqlCommand _cmd;
        private MySqlDataAdapter _mySqlDataAdapter;

        public ShowReportForm(string connectionString)
        {
            InitializeComponent();
            _connectionString = connectionString;
        }

        public void setReportToShow(int reportType, int ID, bool configPrice, bool configWeight, string branchName)
        {
            _reportType = reportType;
            _ID = ID;
            _configPrice = configPrice;
            _configWeight = configWeight;
            _branchName = branchName;
        }

        public void LoadReport()
        {
            try
            {
                _connection = new MySqlConnection(_connectionString);
                _connection.Open();

                _cmd = _connection.CreateCommand();
                _mySqlDataAdapter = new MySqlDataAdapter();

                switch (_reportType)
                {
                    case ORDERFORM_REPORT:
                        LoadOrderFormReport();
                        break;
                    case REQUISITIONFORM_REPORT:
                        LoadRequisitionFormReport();
                        break;
                    case TAXDELIVERYFORM_REPORT:
                        LoadTaxDeliveryFormReport();
                        break;
                    case RECEIPTFORM_REPORT:
                        LoadReceiptFormReport();
                        break;
                    //case MERCHANDISESUMMARYFORM_REPORT:
                        //break;
                    default:
                        break;
                }

                ReportFormViewer.ToolPanelView = CrystalDecisions.Windows.Forms.ToolPanelViewType.None;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                _connection.Close();
            }
        }

        private void LoadOrderFormReport()
        {
            OrderFormDataSet ds = new OrderFormDataSet();
            ds.Clear();

            _cmd.CommandText = "SELECT Quantity, Classifier, Weight, ProductName, Dimension, PricePerUnit, UnitName " +
                            "FROM orderiddescription WHERE OrderID = " + _ID;
            _mySqlDataAdapter.SelectCommand = _cmd;
            _mySqlDataAdapter.Fill(ds, "orderiddescription");

            foreach (DataRow dtRow in ds.Tables["orderiddescription"].Rows)
            {
                string description = dtRow["ProductName"].ToString() + " " + dtRow["Dimension"].ToString();
                dtRow["ProductName"] = description;
            }

            _cmd.CommandText = "SELECT * FROM orderidsellername WHERE OrderID = " + _ID;
            _mySqlDataAdapter.SelectCommand = _cmd;
            _mySqlDataAdapter.Fill(ds, "orderidsellername");

            DateTime orderDate = DateTime.Parse(ds.Tables["orderidsellername"].Rows[0][2].ToString());
            ds.Tables["orderidsellername"].Rows[0][2] = ThaiWordsGenerator.ToThaiDate(orderDate);

            int orderID = Int32.Parse(ds.Tables["orderidsellername"].Rows[0][0].ToString());
            ds.Tables["orderidsellername"].Rows[0][0] = orderID.ToString("D6");

            OrderFormReport orderFormReport = new OrderFormReport();
            orderFormReport.SetDataSource(ds);

            ParameterValues paramValues = new ParameterValues();
            ParameterDiscreteValue paramDiscretePriceValue = new ParameterDiscreteValue();
            ParameterDiscreteValue paramDiscreteWeightValue = new ParameterDiscreteValue();

            paramDiscretePriceValue.Value = _configPrice;
            paramDiscreteWeightValue.Value = _configWeight;

            orderFormReport.SetParameterValue("HidePrice", paramDiscretePriceValue);
            orderFormReport.SetParameterValue("HideWeight", paramDiscreteWeightValue);

            ReportFormViewer.ReportSource = orderFormReport;

            
        }

        private void LoadRequisitionFormReport()
        {
            RequisitionFormDataSet ds = new RequisitionFormDataSet();
            ds.Clear();

            _cmd.CommandText = "SELECT Quantity, Classifier, Weight, ProductName, Dimension, PricePerUnit, UnitName " +
                            "FROM requisitioniddescription WHERE RequisitionID = " + _ID;
            _mySqlDataAdapter.SelectCommand = _cmd;
            _mySqlDataAdapter.Fill(ds, "requisitioniddescription");

            foreach (DataRow dtRow in ds.Tables["requisitioniddescription"].Rows)
            {
                string description = dtRow["ProductName"].ToString() + " " + dtRow["Dimension"].ToString();
                dtRow["ProductName"] = description;
            }

            _cmd.CommandText = "SELECT * FROM requisitionidcustomername WHERE RequisitionID = " + _ID;
            _mySqlDataAdapter.SelectCommand = _cmd;
            _mySqlDataAdapter.Fill(ds, "requisitionidcustomername");

            DateTime orderDate = DateTime.Parse(ds.Tables["requisitionidcustomername"].Rows[0][2].ToString());
            ds.Tables["requisitionidcustomername"].Rows[0][2] = ThaiWordsGenerator.ToThaiDate(orderDate);

            int requisitionID = Int32.Parse(ds.Tables["requisitionidcustomername"].Rows[0][0].ToString());
            ds.Tables["requisitionidcustomername"].Rows[0][0] = requisitionID.ToString("D6");

            _cmd.CommandText = "SELECT Name, Address FROM customer WHERE CustomerID = \'" +
                ds.Tables["requisitionidcustomername"].Rows[0][1].ToString() + "\'";
            _mySqlDataAdapter.SelectCommand = _cmd;
            _mySqlDataAdapter.Fill(ds, "customer");

            RequisitionFormReport requisitionFormReport = new RequisitionFormReport();
            requisitionFormReport.SetDataSource(ds);

            ParameterValues paramValues = new ParameterValues();
            ParameterDiscreteValue paramDiscretePriceValue = new ParameterDiscreteValue();
            ParameterDiscreteValue paramDiscreteWeightValue = new ParameterDiscreteValue();
            
            paramDiscretePriceValue.Value = _configPrice;
            paramDiscreteWeightValue.Value = _configWeight;

            requisitionFormReport.SetParameterValue("HidePrice", paramDiscretePriceValue);
            requisitionFormReport.SetParameterValue("HideWeight", paramDiscreteWeightValue);

            ReportFormViewer.ReportSource = requisitionFormReport;
        }

        private void LoadInvoiceFormReport(InvoiceFormDataSet ds)
        {
            ds.Clear();

            _cmd.CommandText = "SELECT Quantity, Classifier, Weight, ProductName, Dimension, PricePerUnit, UnitName " +
                            "FROM invoiceiddescription WHERE InvoiceID = " + _ID;
            _mySqlDataAdapter.SelectCommand = _cmd;
            _mySqlDataAdapter.Fill(ds, "invoiceiddescription");

            foreach (DataRow dtRow in ds.Tables["invoiceiddescription"].Rows)
            {
                string description = dtRow["ProductName"].ToString() + " " + dtRow["Dimension"].ToString();
                dtRow["ProductName"] = description;
            }

            _cmd.CommandText = "SELECT * FROM invoiceidcustomername WHERE InvoiceID = " + _ID;
            _mySqlDataAdapter.SelectCommand = _cmd;
            _mySqlDataAdapter.Fill(ds, "invoiceidcustomername");

            DateTime orderDate = DateTime.Parse(ds.Tables["invoiceidcustomername"].Rows[0][2].ToString());
            ds.Tables["invoiceidcustomername"].Rows[0][2] = ThaiWordsGenerator.ToThaiDate(orderDate);

            int invoiceID = Int32.Parse(ds.Tables["invoiceidcustomername"].Rows[0][0].ToString());
            ds.Tables["invoiceidcustomername"].Rows[0][0] = invoiceID.ToString("D6");

            _cmd.CommandText = "SELECT Name, Address, TaxID FROM customer WHERE CustomerID = \'" +
                ds.Tables["invoiceidcustomername"].Rows[0][1].ToString() + "\'";
            _mySqlDataAdapter.SelectCommand = _cmd;
            _mySqlDataAdapter.Fill(ds, "customer");

            int customerID = Int32.Parse((ds.Tables["invoiceidcustomername"]).Rows[0]["CustomerID"].ToString());
            string newCustomerID = customerID.ToString("D3") + " " + _branchName;
            (ds.Tables["invoiceidcustomername"]).Rows[0]["CustomerID"] = newCustomerID;
        }

        private void LoadTaxDeliveryFormReport()
        {
            InvoiceFormDataSet ds = new InvoiceFormDataSet();

            LoadInvoiceFormReport(ds);

            TaxDeliveryFormReport taxDeliveryFormReport = new TaxDeliveryFormReport();
            taxDeliveryFormReport.SetDataSource(ds);
            ReportFormViewer.ReportSource = taxDeliveryFormReport;

            ParameterValues paramValues = new ParameterValues();
            ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();
            if(_configPrice)
                paramDiscreteValue.Value = "ต้นฉบับ/Original";
            else
                paramDiscreteValue.Value = "สำเนา/Copy";
            paramValues.Add(paramDiscreteValue);
            taxDeliveryFormReport.DataDefinition.ParameterFields["OriginalOrCopy"].ApplyCurrentValues(paramValues);
        }

        private void LoadReceiptFormReport()
        {
            InvoiceFormDataSet ds = new InvoiceFormDataSet();

            LoadInvoiceFormReport(ds);

            ReceiptFormReport receiptFormReport = new ReceiptFormReport();
            receiptFormReport.SetDataSource(ds);
            ReportFormViewer.ReportSource = receiptFormReport;

            ParameterValues paramValues = new ParameterValues();
            ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();
            if (_configPrice)
                paramDiscreteValue.Value = "ต้นฉบับ/Original";
            else
                paramDiscreteValue.Value = "สำเนา/Copy";
            paramValues.Add(paramDiscreteValue);
            receiptFormReport.DataDefinition.ParameterFields["OriginalOrCopy"].ApplyCurrentValues(paramValues);
        }

        public void LoadMerchandiseSummaryReport(DataTable dt, string month, string year, 
            string productName, string dimension, string unitName)
        {
            MerchandiseSummaryDataSet ds = new MerchandiseSummaryDataSet();

            for (int i = dt.Rows.Count - 1; i >= 0; i-- )
            {
                DataRow dtRow = dt.Rows[i];
                ds.Tables["merchandisesummary"].ImportRow(dtRow);
            }

            ds.Tables["headerdata"].Rows.Add(month, year, productName, dimension, unitName);

            MerchandiseSummaryFormReport merchandiseSummaryFormReport = new MerchandiseSummaryFormReport();
            merchandiseSummaryFormReport.SetDataSource(ds);

            ReportFormViewer.ReportSource = merchandiseSummaryFormReport;
        }
    }
}
